<?php

namespace app\publics\controller\api;
use app\ApiController;
use app\mainadmin\model\MessageModel;
use app\mainadmin\model\MessageUserModel;

/*------------------------------------------------------ */
//-- 站内消息相关API
/*------------------------------------------------------ */

class Message extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
    }

    /*------------------------------------------------------ */
    //-- 获取消息列表
    /*------------------------------------------------------ */
    public function getList()
    {
        $state = input('state','waitCheck','trim');
        $MessageModel = new MessageModel();
        $this->sqlOrder = 'send_time DESC';
        if ($state == 'my'){
            $MessageUserModel = new MessageUserModel();
            $where = [];
            $where[] = ['user_id','=',$this->userInfo['user_id']];
            $where[] = ['send_time','<',time()];
            $where[] = ['show_end_time','>',time()];
            $data = $this->getPageList($MessageUserModel, $where,'',8);
            foreach ($data['list'] as $key=>$row){
                $row['send_time'] = dateTpl($row['send_time']);
                if ($row['type'] == 0){
                    $message = $MessageModel->where('message_id',$row['ext_id'])->find();
                    if ($message['article_id'] > 0){
                        $row['ext_id'] = $message['article_id'];
                    }else{
                        $row['ext_id'] = 0;
                    }
                }
                $data['list'][$key] = $row;
            }
            return $this->success($data);
        }else{//系统公告
            $where = [];
            $where[] = ['type','=','all'];
            $data = $this->getPageList($MessageModel, $where,'',8);
            foreach ($data['list'] as $key=>$row){
                $row['send_time'] = dateTpl($row['send_time']);
                $data['list'][$key] = $row;
            }
        }
        return $this->success($data);
    }



}

